#include "abclistview.h"
#include "ui_abclistview.h"
#include "abcdialog.h"
#include "common/tdebug.h"
#include "ttableview.h"
#include "tlistview.h"
#include "abcdialog.h"


AbcListView::AbcListView(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::AbcListView)
{
    ui->setupUi(this);
    mABCDialog=new ABCDialog(this);
    memset(mLetterPos,-1,28);
#ifdef STRABC
    connect(ui->bar,SIGNAL(ABCChanged(QString)),this,SLOT(updateDilog(QString)));
#else
    connect(ui->bar,SIGNAL(ABCChanged(char)),this,SLOT(updateDilog(char)));
#endif

    connect(ui->bar,SIGNAL(ABCTipStart()),this,SLOT(dialogShow()));
    connect(ui->bar,SIGNAL(ABCTipEnd()),this,SLOT(dialogHide()));
    //ui->listView->verticalScrollBar()->hide();

    mABCDialog->hide();

    mModel=NULL;
    mListView = ui->widget->getListView();

}

void AbcListView::setBarSize(int a, int b)
{
    ui->horizontalLayout->setStretch(0,a);
    ui->horizontalLayout->setStretch(1,b);
}

void AbcListView::setBarEnable(bool b)
{
    if(b)
    {
        ui->bar->show();
    }
    else
    {
        ui->bar->hide();
    }
}

TListView * AbcListView::getListView()
{
    return mListView;
}

QLabel *AbcListView::getTitleLabel(const QString &str)
{
    return ui->widget->getLabel(str);
}

void AbcListView::setModel(QAbstractListModel *model,int num)
{


    mModel= model;

    mListView->setModel(mModel);

    mCheckNum = num;
}

void AbcListView::setCheckNum(int num)
{
    mCheckNum = num;
}

void AbcListView::setABCDialogPos(int x, int y)
{
    mABCDialog->move(x,y);
}

#ifdef STRABC
int AbcListView::getSelect(QString str)
{

    if(mModel==NULL)
    {
        mModel=(QAbstractListModel*)(ui->tableView->model());
    }
    for(int i=0;i<mModel->rowCount();i++)
    {
        //if(mModel->index(i).data(mCheckNum).toString().toUtf8().toUpper().at(0) >= str.toUtf8())
        //if(mModel->index(i).data(mCheckNum).toString().toUtf8().toUpper().left(1) >= str.toUtf8())
        if(mModel->index(i).data(mCheckNum).toString().at(0).toUpper() >= str.at(0))
        {
            return i;
        }
    }
    return -1;
}
#else
int AbcListView::getSelect(char c)
{
    if(c<'A')c = 'A'-1;
    if(c>'z')c = 'Z'+1;
    if(mModel==NULL)
    {
        mModel=(QAbstractListModel*)(mListView->model());
    }
    if(mLetterPos[c-'A'+1]==-1)
    {
        for(int i=0,t=mModel->rowCount();i<t;i++)
        {
            if(mModel->index(i).data(mCheckNum).toString().at(0).toUpper().toLatin1()>=c)
            {
                mLetterPos[c-'A'+1] = i;
                return i;
            }
        }
    }
    else
    {
        return mLetterPos[c-'A'+1];
    }
    return -1;
}
#endif


void AbcListView::setItemDelegate ( QAbstractItemDelegate * delegate )
{
    mListView->setItemDelegate(delegate);
}

void AbcListView::setShowItems(const QList<ListViewShowItem> &item)
{
    ui->widget->setShowItems(item);
}

#ifdef STRABC
void AbcListView::updateDilog(QString str)
{
//    QString str = ABCListViewBar::tr(ABCListViewBar::mChar[indx]);
//    char trs= str[0].toLatin1();

    mABCDialog->UpdateStr(str);

    int r=getSelect(str);
    QModelIndex ind=mModel->index(r);
    ui->tableView->setCurrentIndex(ind);
    ui->tableView->scrollTo(ind,QAbstractItemView::PositionAtTop);
    mABCDialog->repaint();
}
#else
void AbcListView::updateDilog(char c)
{
    mABCDialog->UpdateChar(c);
    int r=getSelect(c);
    QModelIndex ind=mModel->index(r);
    mListView->setCurrentIndex(ind);
    mListView->scrollTo(ind,QAbstractItemView::PositionAtTop);
    mABCDialog->repaint();
}
#endif

void AbcListView::dialogShow()
{
    int x = (width() - mABCDialog->width())/2;
    int y = (height() - mABCDialog->height())/2;
    mABCDialog->move(x,y);
    mABCDialog->show();
}
void AbcListView::dialogHide()
{
    mABCDialog->hide();
}

AbcListView::~AbcListView()
{
    delete ui;
}
